// 新增util内容方便全局调用
import Cache from '@/utils/cache';
export default {
	// 获取地理位置;
	$L: {
		async getLocation() {
			return new Promise(async (resolve, reject) => {
				var status = "";
				// #ifdef APP-PLUS
				 status = await this.checkPermission();
				if (status !== 1) {
					resolve(status);
					return;
				}
				// #endif
				// #ifdef MP-WEIXIN || MP-TOUTIAO || MP-QQ
				status = await this.getSetting();
				if (status === 2) {
					this.openSetting();
					resolve(status);
					return;
				}
				// #endif
				
				let location = this.doGetLocation();
				resolve(location);
			})
			
		},
		doGetLocation() {
			return new Promise((resolve, reject) => {
				uni.getLocation({
						success: (res) => {
							
							uni.removeStorageSync('lng');
							uni.removeStorageSync('lat');
							
							let re = {
								latitude:Number(res.latitude.toFixed(6)),
								longitude:Number(res.longitude.toFixed(6))
							}
							uni.setStorageSync('lng', re.longitude);
							uni.setStorageSync('lat', re.latitude);
							resolve(res);
						},
						complete: (res) => {
							let re = {
								latitude:Number(res.latitude.toFixed(6)),
								longitude:Number(res.longitude.toFixed(6))
							}
							uni.setStorageSync('lat', re.latitude);
							uni.setStorageSync('lng', re.longitude);
							console.log("定位complete：：：",re);
							resolve(res);
						},
						fail: (err) => {
							console.log("位置信息获取失败：：",err);
							// #ifdef MP-BAIDU
							if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
								this.openSetting();
							}
							// #endif
							// #ifndef MP-BAIDU
							if (err.errMsg.indexOf("auth deny") >= 0) {
								uni.showToast({
									title: "访问位置被拒绝"
								})
							} else {
								uni.showToast({
									title: err.errMsg
								})
							}
							// #endif
							reject(err);
						}
					})
			})
			
		},
		getSetting: function() {
			return new Promise((resolve, reject) => {
				uni.getSetting({
					success: (res) => {
						if (res.authSetting['scope.userLocation'] === undefined) {
							resolve(0);
							return;
						}
						if (res.authSetting['scope.userLocation']) {
							resolve(1);
						} else {
							resolve(2);
						}
					}
				});
			});
		},
		openSetting: function() {
			uni.openSetting({
				success: (res) => {
					if (res.authSetting && res.authSetting['scope.userLocation']) {
						this.doGetLocation();
					}
				},
				fail: (err) => {}
			})
		},
		async checkPermission() {
			let status = permision.isIOS ? await permision.requestIOS('location') :
				await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');
			if (status === null || status === 1) {
				status = 1;
			} else if (status === 2) {
				uni.showModal({
					content: "系统定位已关闭",
					confirmText: "确定",
					showCancel: false,
					success: function(res) {}
				})
			} else if (status.code) {
				uni.showModal({
					content: status.message
				})
			} else {
				uni.showModal({
					content: "需要定位权限",
					confirmText: "设置",
					success: function(res) {
						if (res.confirm) {
							permision.gotoAppSetting();
						}
					}
				})
			}
			return status;
		},
	},
	
	/*
	 * 合并数组
	 */
	SplitArray(list, sp) {
		if (typeof list != 'object') return [];
		if (sp === undefined) sp = [];
		for (var i = 0; i < list.length; i++) {
			sp.push(list[i]);
		}
		return sp;
	},
	
}

